package com.ruoyi.yl.common;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * BasePageRequestVO
 * 基础分页入参
 *
 * @author lsw
 * @date 2012/07/09 11:33
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("基础分页request")
public class BasePageReqVO implements Serializable {

    private static final Integer DEFAULT_PAGE = 1;
    private static final Integer DEFAULT_PAGE_SIZE = 10;

    @ApiModelProperty("页码")
    private Integer pageNo;

    @ApiModelProperty("分页大小")
    private Integer pageSize;


    public <T> Page<T> toPageRequest() {
        validate();
        return new Page<>(Math.max(pageNo, 1), pageSize);
    }

    public <T> Page<T> toPageRequest(String sortField) {
        Page<T> page = new Page<>();
        if (sortField == null || "".equals(sortField)) {
            return this.toPageRequest();
        } else {
           /// page.setDesc(sortField);
        }
        validate();
        page.setCurrent(pageNo);
        page.setSize(pageSize);
        return page;
    }

    public BasePageReqVO validate() {
        if (pageNo == null || pageNo <= 0) {
            this.pageNo = DEFAULT_PAGE;
        }
        if (pageSize == null || pageSize < -1) {
            this.pageSize = DEFAULT_PAGE_SIZE;
        }
        return this;
    }
}
